package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.enums.system.UserTypeEnum;
import com.goods.common.model.business.Supplier;
import com.goods.common.model.system.Department;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.DepartmentVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/2 22:14 周五
 * description:
 */
@Service
public class SupplierServiceimpl implements SupplierService {


    @Autowired
    private SupplierMapper supplierMapper;



    //物资来源分页列表展示
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, String name) {

        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Department.class);
        if (name!= null && !"".equals(name)) {
            o.createCriteria().andLike("name", "%" + name + "%");
        }
        List<Supplier> supplierList = supplierMapper.selectByExample(o);
        //转vo
        List<SupplierVO> supplierVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(supplierList)) {
            for (Supplier supplier : supplierList) {
                SupplierVO s = new SupplierVO();
                BeanUtils.copyProperties(supplier, s);

                supplierVOS.add(s);
            }
        }
        PageInfo<Supplier> info = new PageInfo<>(supplierList);
        return new PageVO<>(info.getTotal(), supplierVOS);

    }


    //添加
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier =new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setCreateTime(new Date());
        supplierMapper.insert(supplier);
    }


    //删除物资来源
    @Override
    public void delete(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier !=null){
            supplierMapper.delete(supplier);
        }
    }


    //编辑物资来源
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        return SupplierConverter.converterToSupplierVO(supplier);
    }


    //更新物资来源
    @Override
    public void update(Long id, SupplierVO supplierVO) {
          Supplier supplier = new Supplier();
          BeanUtils.copyProperties(supplierVO, supplier);
          supplier.setId(id);
          supplier.setModifiedTime(new Date());
          supplierMapper.updateByPrimaryKey(supplier);
    }
}
